package com.novel.controller.system;

import com.novel.controller.other.BaseController;
import com.novel.pojo.system.Order;
import com.novel.service.OrderService;
import com.novel.utils.OrderNoUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.UUID;

@Slf4j
@Controller
@RequestMapping("order")
public class OrderController extends BaseController {

    @Autowired
    private OrderService orderService;

    /**
     * 支付订单
     */
    @PostMapping("pay")
    public boolean payOrder(String orderNo) {
        return orderService.payOrder(orderNo);
    }

    /**
     * 创建订单
     */
    @PostMapping("create")
    public boolean createOrder(Integer amount, Double cost) {
        //创建订单信息
        Order order = new Order();
        order.setUserId(getUserInformation().getUserId());
        order.setCreatedIp(getUserIpAddress());
        order.setCost(cost);
        order.setBalance(amount);
        order.setId(UUID.randomUUID().toString());
        order.setOrderNo(OrderNoUtils.generate());
        order.setCreatedTime(new Date());
        order.setStatus(0);
        //保存订单信息
        return orderService.createdOrder(order);
    }


}